package com.useeinfo.oa.modules.base.entity;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.entity.SuperClassEntity;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.base.enums.ResourceTypeEnum;

import javax.persistence.*;
import java.util.Objects;

/**
 * @Auther: xudong.sun
 * @Date: 2018/11/30 13:30
 * @Description:资源表
 */
@Entity
@Table(name = "resource")
public class Resource extends SuperClassEntity<Resource> {

    /**
     * 文件路径
     */
    private String path;

    /**
     * 资源名称
     */
    private String resourceName;

    /**
     * 文件类型
     */
    @Enumerated(value = EnumType.STRING)
    @Column(name = "resource_type")
    private ResourceTypeEnum resourceTypeEnum;

    @ManyToOne
    @JoinColumn(name = "enterprise_id")
    private Enterprise enterprise;

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public ResourceTypeEnum getResourceTypeEnum() {
        return resourceTypeEnum;
    }

    public void setResourceTypeEnum(ResourceTypeEnum resourceTypeEnum) {
        this.resourceTypeEnum = resourceTypeEnum;
    }

    public String getResourceName() {
        return resourceName;
    }

    public void setResourceName(String resourceName) {
        this.resourceName = resourceName;
    }

    public Enterprise getEnterprise() {
        return enterprise;
    }

    public void setEnterprise(Enterprise enterprise) {
        this.enterprise = enterprise;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Resource resource = (Resource) o;
        return Objects.equals(path, resource.path) &&
                Objects.equals(resourceName, resource.resourceName) &&
                resourceTypeEnum == resource.resourceTypeEnum &&
                Objects.equals(enterprise, resource.enterprise);
    }

    @Override
    public int hashCode() {
        return Objects.hash(path, resourceName, resourceTypeEnum, enterprise);
    }

    @Override
    public JSONObject toJSONObject() {
        JSONObject jsonObject = super.toJSONObject();
        jsonObject.put("path", StringConverters.null2String(getPath()));
        jsonObject.put("resourceName", StringConverters.null2String(getResourceName()));
        jsonObject.put("resourceType", StringConverters.null2String(getResourceTypeEnum().getName()));
        return jsonObject;
    }
}
